local skel = fk.CreateSkill {
  name = "emo__zhence",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["emo__zhence"] = "侦测",
  [":emo__zhence"] = "锁定技，你跳过摸牌阶段；弃牌阶段你自动弃置左侧手牌；当你失去手牌时，摸至4张；当你获得手牌时，弃置左侧手牌至4张。",

  ["$emo__zhence1"] = "I need to skip drawcard phase.",
  ["$emo__zhence2"] = "I would draw card to 4.",
  ["$emo__zhence3"] = "I need to discard to 4.",
  ["$emo__zhence4"] = "Discard protocol... engaged.",
}

skel:addEffect(fk.EventPhaseChanging, {
  anim_type = "negative",
  audio_index = 1,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return target == player and data.phase == Player.Draw
  end,
  on_use = function (self, event, target, player, data)
    data.skipped = true
  end,
})

skel:addEffect(fk.EventPhaseProceeding, {
  anim_type = "negative",
  anim_index = 4,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or data.phase_end then return false end
    return target == player and player.phase == Player.Discard and player:getHandcardNum() > player:getMaxCards()
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    room:delay(600)
    local cards = {}
    local num = player:getHandcardNum() - player:getMaxCards()
    -- 判断不计入手牌上限的部分
    local status_skills = Fk:currentRoom().status_skills[MaxCardsSkill] or Util.DummyTable
    for _, id in ipairs(player.player_cards[Player.Hand]) do
      local check = true
      for _, skill in ipairs(status_skills) do
        if skill:excludeFrom(player, Fk:getCardById(id)) then
          check = false
          break
        end
      end
      if check and not player:prohibitDiscard(Fk:getCardById(id)) then
        table.insert(cards, id)
        if #cards >= num then break end
      end
    end
    if #cards > 0 then
      room:throwCard(cards, skel.name, player, player)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) or player:usedEffectTimes(skel.name) > 99 then return false end
    if player:getHandcardNum() ~= 4 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          return true
        elseif move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = 4 - player:getHandcardNum()
    if num > 0 then
      room:delay(100)
      room:notifySkillInvoked(player, skel.name, "drawcard")
      player:broadcastSkillInvoke(skel.name, 2)
      player:drawCards(num, skel.name)
    elseif num < 0 then
      room:notifySkillInvoked(player, skel.name, "negative")
      player:broadcastSkillInvoke(skel.name, 3)
      -- 判鸡肋！
      local cards = {}
      for _, id in ipairs(player.player_cards[Player.Hand]) do
        if not player:prohibitDiscard(Fk:getCardById(id)) then
          table.insert(cards, id)
          if #cards >= (-num) then break end
        end
      end
      if #cards > 0 then
        room:throwCard(cards, skel.name, player, player)
      end
    end
  end,
})

return skel
